﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace NinjaToolbox.PhysicsSystem
{
    public static class PhysicsLayerDrawer
    {
        public static int Draw(string labelName, SerializedProperty layerConstInfoProp, int layer, ref string[] cacheNameArray)
        {
            var layerConstInfoPropValue = layerConstInfoProp.objectReferenceValue as PhysicsSystem_LayerConstInfo;

            if (cacheNameArray == null || layerConstInfoPropValue.constInfoArray.Length != cacheNameArray.Length)
            {
                cacheNameArray = layerConstInfoPropValue.constInfoArray.Select(m => m.layerName).ToArray();
            }

            var index = Array.FindIndex(layerConstInfoPropValue.constInfoArray, m => m.layer == layer);
            index = Mathf.Max(index, 0);

            index = EditorGUILayout.Popup(new GUIContent(labelName), index, cacheNameArray);

            return layerConstInfoPropValue.constInfoArray[index].layer;
        }
    }
}
